package com.lmonkey.servic;

import com.lmonkey.dao.Basedao;
import com.lmonkey.shop.CATEGORY;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CATAGORYDao {
//    获取所有分类
    public static ArrayList<CATEGORY> selectAll(){
        ArrayList<CATEGORY> list = new ArrayList<CATEGORY>();
        //声明结果集
        ResultSet rs = null;
        //连接数据库
        Connection conn = Basedao.getconn();

        PreparedStatement ps =null;
        try {
                String sql = "select * from lmonkey_category";
                ps = conn.prepareStatement(sql);
                rs = ps.executeQuery();
                while(rs.next()){
                    CATEGORY c = new CATEGORY(
                            rs.getInt("cate_id"),
                            rs.getString("cate_name"),
                            rs.getInt("cate_parent_id")
                    );
                    list.add(c);
                }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public static CATEGORY selectByid(int id){
        CATEGORY c = null;
        //声明字符集
        ResultSet rs = null;
        //连接数据库
        Connection conn = Basedao.getconn();
        PreparedStatement ps = null;

        try{
            String sql = "select * from lmonkey_category where cate_id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while(rs.next()){
                c = new CATEGORY(
                        rs.getInt("cate_id"),
                        rs.getString("cate_name"),
                        rs.getInt("cate_parent_id")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return c;
    }
    public static int  del(int id){
        int count = 0;
        Connection conn = Basedao.getconn();
        ResultSet rs = null;
        PreparedStatement ps = null;
        try {
            String sql = "delete from lmonkey_category where cate_id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
    //如果flag=father则获取父分类如果flag=child则获取子分类
    public static ArrayList<CATEGORY> selectcat(String flag){
        ArrayList<CATEGORY> list = new ArrayList<CATEGORY>();
        //声明结果集
        ResultSet rs = null;
        //连接数据库
        Connection conn = Basedao.getconn();

        PreparedStatement ps =null;
        try {
            String sql = null;
            if (flag!=null && flag.equals("father")){
                sql = "select * from lmonkey_category where cate_parent_id = 0";
            }else{
                sql = "select * from lmonkey_category where cate_parent_id != 0";
            }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                CATEGORY c = new CATEGORY(
                        rs.getInt("cate_id"),
                        rs.getString("cate_name"),
                        rs.getInt("cate_parent_id")
                );
                list.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
